<template>
  <div class="login">
    <div class="login-hd">
      <i @click="goBack" class="iconfont icon-xiangzuo1"></i>
      <span><router-link to="/register">注册</router-link></span>
    </div>
    <form class="box" :model="form">
      <h1 class="login-text">LOGIN</h1>
      <input
        class="input-1"
        type="text"
        name="username"
        v-model="form.username"
        placeholder="username"
      />
      <!-- value="admin" -->
      <input
        class="input-2"
        type="password"
        name="password"
        v-model="form.password"
        placeholder="password"
      />
      <!-- <button></button> -->
      <input class="input-3" @click="goLogin" type="button" value="登录" />
    </form>
    <div class="mask" v-if="show">
      <p class="p-1" v-if="isflag">登录成功！,正在跳转...</p>
      <p class="p-2" v-if="!isflag">登录失败！,稍后重试...</p>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations } from "vuex";
export default {
  name: "login",
  data() {
    return {
      isflag: false,
      show: false,
      form: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    ...mapMutations(["changeLoginToken"]),
    ...mapActions(["getUserInfo", "getLikeId"]),
    showNotify() {
      this.show = true;
      this.isflag = true;
      setTimeout(() => {
        this.show = false;
        this.$router.push("/center");
      }, 2000);
    },
    goLogin() {
      // console.log("用户信息---->", this.form);
      this.axios
        // http://120.78.9.102:8006/api
        .post("http://120.78.9.102:8006/api/login", this.form)
        .then((result) => {
          console.log("登录成功--->", result);
          let token = result.token;
          // 把token保存在vuex
          this.changeLoginToken(token);
          // //调用vuex的函数获取用户信息
          this.getUserInfo();

          // console.log(
          //   "调用vuex的函数获取用户信息---",
          //   this.$store.state.userInfo.id
          // );
          setTimeout(() => {
            console.log(this.$store.state.userInfo, "-----------");
            if (this.$store.state.userInfo != null) {
              // 异步获取当前用户id
              this.getLikeId();
            }
          });

          if (result.status == 200) {
            this.showNotify();
          } else {
            this.show = true;
            this.isflag = false;
            setTimeout(() => {
              this.show = false;
            }, 1000);
          }
        });
    },
    goBack() {
      this.$router.go(-1);
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  width: 375px;
  height: 100%;
  position: fixed;
  font-family: sans-serif;
  // background: url("../assets/imgs/loginpic01.png") no-repeat;
  // background-size: cover;
  // filter: blur(px);
  background: rgb(255, 255, 255);
}
.box {
  width: 300px;
  padding: 40px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #19191900;
  text-align: center;
  box-shadow: 1px 0px 20px 1px #ccc;
  .login-text {
    width: 100%;
    color: rgb(0, 0, 0);
    text-transform: uppercase;
    font-weight: 500;
    font-size: 24px;
  }
  .input-1,
  .input-2 {
    width: 200px;
    height: 40px;
    font-size: 16px;
    background: none;
    display: block;
    margin: 20px auto;
    text-align: center;
    border: 2px solid #3498db;
    outline: none;
    color: rgb(0, 0, 0);
    border-radius: 24px;
    transition: 0.25s;
  }
  .input-1:focus {
    width: 280px;
    border-color: #2ecc71;
  }
  .input-2:focus {
    width: 280px;
    border-color: #2ecc71;
  }
  .input-3 {
    width: 100px;
    height: 40px;
    font-size: 16px;
    background: none;
    display: block;
    margin: 20px auto;
    text-align: center;
    border: 2px solid #2ecc71;
    box-sizing: border-box;
    outline: none;
    color: rgb(0, 0, 0);
    border-radius: 24px;
    transition: 0.25s;
  }
}
.login-hd {
  width: 352px;
  height: 40px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  i {
    font-size: 30px;
    color: rgb(0, 0, 0);
  }
  span {
    a {
      font-size: 24px;
      color: rgb(0, 0, 0);
    }
  }
}
.mask {
  width: 100%;
  position: fixed;
  bottom: 10px;
  animation: masked 3s linear;
  .p-1 {
    width: 100%;
    text-align: center;
    font-size: 22px;
    color: rgb(17, 120, 255);
  }
  .p-2 {
    width: 100%;
    text-align: center;
    font-size: 22px;
    color: rgb(255, 0, 0);
  }
}
@keyframes masked {
  0% {
    transform: scale(0.6);
  }
  25% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.2);
  }
  75% {
    transform: scale(0.6);
  }
  100% {
    transform: scale(0);
  }
}
</style>